package com.xmtlxx.web.controller.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.web.controller.tms.service.IWaybillLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.WaybillLog;
import com.xmtlxx.tms.domain.bo.WaybillLogBo;
import com.xmtlxx.tms.domain.vo.WaybillLogVo;
import com.xmtlxx.tms.mapper.WaybillLogMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 运单日志Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WaybillLogServiceImpl implements IWaybillLogService {

    private final WaybillLogMapper baseMapper;

    /**
     * 查询运单日志
     *
     * @param id 运单日志主键
     * @return 运单日志
     */
    @Override
    public WaybillLogVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectWaybillLogVoByID(id);
    }

    /**
     * 查询运单日志列表
     *
     * @param bo 运单日志
     * @return 运单日志
     */
    @Override
    public TableDataInfo<WaybillLogVo> queryPageList(WaybillLogBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<WaybillLog> lqw = buildQueryWrapper(bo);
       // Page<WaybillLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<WaybillLogVo> result = baseMapper.selectPageWaybillLogList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询运单日志列表
     *
     * @param bo 运单日志
     * @return 运单日志
     */
    @Override
    public List<WaybillLogVo> queryList(WaybillLogBo bo) {
       // LambdaQueryWrapper<WaybillLog> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectWaybillLogList(bo);
    }

    private LambdaQueryWrapper<WaybillLog> buildQueryWrapper(WaybillLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WaybillLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTrackId()), WaybillLog::getTrackId, bo.getTrackId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorId()), WaybillLog::getOperatorId, bo.getOperatorId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), WaybillLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), WaybillLog::getType, bo.getType());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            WaybillLog::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), WaybillLog::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增运单日志
     *
     * @param bo 运单日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(WaybillLogBo bo) {
        WaybillLog add = BeanUtil.toBean(bo, WaybillLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改运单日志
     *
     * @param bo 运单日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(WaybillLogBo bo) {
        WaybillLog update = BeanUtil.toBean(bo, WaybillLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WaybillLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除运单日志
     *
     * @param ids 需要删除的运单日志主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
